<?php
namespace app\controllers\api\fans;

use fate\http\Request;

use app\libs\LibUser;
use app\models\Fans;

class FollowController extends \fate\web\PureController {

    public function run() {
        global $_G;

        // 登录用户关注其他人
        $fansUid = LibUser::getLoginUid();
        if('' === $fansUid) {
            $this->ajaxReturn(null, 200001);
            exit;
        }

        $req = Request::getInstance();
        $uid = $req->getParameter('uid');

        if(!is_numeric($uid)) {
            $this->ajaxReturn(null, 300002);
            exit;
        }

        if($uid === $fansUid) {
            $this->ajaxReturn(null, 100011);
            exit;
        }

        $model = new Fans();

        // 频率限制 20 秒一条
        $last = $model->getOneByCondition(
            'post_time',
            'uid=' . $uid . ' and fans_uid=' . $fansUid,
            'id desc');

        // 没有数据直接插入
        if(!$last) {
            $model->follow($uid, $fansUid);
            $this->ajaxReturn(null);
            return;
        }

        // 有数据做判断
        if($_G['now_time'] - $last['post_time'] < 20) {
            $this->ajaxReturn(null, 300001);
            exit;
        }

        $model->follow($uid, $fansUid);
        $this->ajaxReturn(null);
    }

}
